﻿jQuery.fn.extend({
    DomKey: function (code) {
        var evt, evt2, $el = this;
        if (document.createEvent) { // Most browsers
            evt = document.createEvent('KeyboardEvent');
            evt2 = document.createEvent('KeyboardEvent');
            if ($el.length && evt) {
                evt.initKeyboardEvent(
                        'keydown',
                        true, // Click events bubble
                        true, // and they can be cancelled
                        document.defaultView || document.parentWindow, // Use the default view
                        'U+0051',
                        0,
                        1, 1, 1, 1,
                        code, code, code
                    ); // Click events don't have any targets other than
                // the recipient of the click
                //				evt.keyCode = options.keyCode;
                //				evt.keyPress = options.keyCode;
                evt['charCode'] = code;
                evt['which'] = code;
                evt['keyCode'] = code;
                $el[0].dispatchEvent(evt);
                console.dir(evt);

                evt2.initKeyboardEvent(
                        'keyup',
                        true, // Click events bubble
                        true, // and they can be cancelled
                        document.defaultView || document.parentWindow, // Use the default view
                        'U+0051',
                        0,
                        0, // Just a single click
                        0, // Don't bother with co-ordinates
                        0,
                        0,
                        0,
                        0
                //					'e'.charCodeAt(0),
                //					'e'.charCodeAt(0)
                    ); // Click events don't have any targets other than
                evt2.charCode = code;
                evt2.which = code;
                evt2.keyCode = code;
                $el[0].dispatchEvent(evt2);
                console.dir(evt2);
            }
        }
        else if (document.createEventObject) { // IE Browser
            $el[0].click();
        }
        else
            throw 'DomTrigger unable to create event';
        return this;
    }
});
        